<?php
namespace Index\Controller;
use Think\Controller;
use Think\Verify;
class LoginController extends Controller {
    public function indexAction(){
        
        if(IS_POST){
        	$username = I('post.user_name');
        	$password = md5(I('post.password'));

//了解数据库取数据
            $Model = M();
            $userNamerInfo = $Model->query("SELECT * FROM user where username='{$username}'");
            $userMailInfo = $Model->query("SELECT * FROM user where mail='{$username}'");


            //判断用户是否存在
            if(!empty($userNamerInfo) || !empty($userMailInfo)){

                $userInfo = empty($userNamerInfo) ? $userMailInfo[0] : $userNamerInfo[0];

                if($userInfo['password']==$password){
                    $_SESSION['username'] = $userInfo['username'];
                    $_SESSION['uid'] = $userInfo['id'];
                    $_SESSION['gid'] = $userInfo['gid'];
                    $_SESSION['face_url'] = $userInfo['face_url'];
                    $_SESSION['password'] = $userInfo['password'];
                    $this->success('登录成功',U("Index/index"),2);
                }else{
                    $this->error('用户名或密码不正确','',2);
                }

            }else{
                $this->error('用户名或密码不正确','',2);
            }

        }else{
        	$this->display('index');
        }
    }

    public function logoutAction(){
        session_unset();
        session_destroy();
        $this->success('退出成功',U("Index/index"),2);
    }

}